home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / MacHack'90 Proceedings / John Norstad / Reusable Code / Source / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-10  |  8.2 KB  |  320 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     help.c - Help Window Manager.
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is 
  6.     granted to use this code in your own projects, provided you give credit 
  7.     to both John Norstad and Northwestern University in your about box or 
  8.     document.
  9. _____________________________________________________________________*/
  10.  
  11.  
  12. #pragma load "precompile"
  13. #include "hlp.h"
  14. #include "utl.h"
  15. #include "rez.h"
  16. #include "glob.h"
  17. #include "wstm.h"
  18. #include "misc.h"
  19. #include "help.h"
  20. #include "main.h"
  21. #include "misc.h"
  22. #include "unmount.h"
  23. #include "vol.h"
  24.  
  25. #pragma segment help
  26.  
  27. /*______________________________________________________________________
  28.  
  29.     Global Variables.
  30. _____________________________________________________________________*/
  31.  
  32.  
  33. static WindowPtr            HelpWindow = nil;        /* ptr to help window */
  34. static WindowRecord        HelpWRecord;            /* help window record */
  35. static WindowObject        HelpWindObject;        /* help window object */
  36.  
  37. /*______________________________________________________________________
  38.  
  39.     Click - Process Mouse Down Event.
  40.     
  41.     Entry:        where = mouse down location, local coords.
  42.                     modifiers = modifier keys.
  43. _____________________________________________________________________*/
  44.  
  45. static void Click (Point where, short modifiers)
  46.  
  47. {
  48. #pragma unused (modifiers)
  49.  
  50.     hlp_Click(where);
  51. }
  52.  
  53. /*______________________________________________________________________
  54.  
  55.     Help - Process Mouse Down Event in Help Mode.
  56.     
  57.     Entry:        where = mouse down location, local coords.
  58. _____________________________________________________________________*/
  59.  
  60.  
  61. static void Help (Point where)
  62.  
  63. {
  64. #pragma unused (where)
  65.  
  66.     help_Open(tagHelpWind);
  67. }
  68.     
  69. /*______________________________________________________________________
  70.  
  71.     Close - Close Window.
  72. _____________________________________________________________________*/
  73.  
  74.  
  75. static void Close (void)
  76.  
  77. {
  78.     Prefs.helpState.moved = HelpWindObject.moved;
  79.     wstm_Save(HelpWindow, &Prefs.helpState);
  80.     Prefs.helpScrollPos = hlp_GetScrollPos();
  81.     hlp_Close();
  82.     CloseWindow(HelpWindow);
  83.     HelpWindow = nil;
  84. }
  85.     
  86. /*______________________________________________________________________
  87.  
  88.     Save - Process Save Command.
  89.     
  90.     Exit:        function result = true (nobody cares if the user canceled!)
  91. _____________________________________________________________________*/
  92.  
  93.  
  94. static Boolean Save (void)
  95.  
  96. {
  97.     Str255            prompt;            /* SFPutFile prompt string */
  98.     Str255            defName;            /* default name for help save dlog */
  99.     OSErr                rCode;            /* result code */
  100.     Str255            rCodeStr;        /* result code as a string */
  101.     long                savedTrapAddr;    /* saved UnmountVol trap adress */
  102.     
  103.     /* Get the prompt string and default file name. */
  104.     
  105.     GetIndString(prompt, strListID, abPromptStr);
  106.     GetIndString(defName, strListID, abDefNameStr);
  107.     
  108.     /* If scanning, patch the UnmountVol trap to prevent volume unmounting. */
  109.     
  110.     if (Scanning) {
  111.         savedTrapAddr = NGetTrapAddress(_UnmountVol, ToolTrap);
  112.         NSetTrapAddress((long)UNMOUNT, _UnmountVol, ToolTrap);
  113.     };
  114.     
  115.     /* Call hlp_Save to save the file. */
  116.     
  117.     rCode = hlp_Save(prompt, defName, Prefs.docCreator, MenuPick);
  118.     
  119.     /* Handle errors. */
  120.     
  121.     if (rCode) {
  122.         if (rCode == dskFulErr) {
  123.             (void) utl_StopAlert(diskFullID, nil, 0);
  124.         } else {
  125.             NumToString(rCode, rCodeStr);
  126.             ParamText(rCodeStr, nil, nil, nil);
  127.             utl_StopAlert(unexpectedSaveID, nil, 0);
  128.         };
  129.     };
  130.     
  131.     /* If scanning, restore the UnmountVol trap. */
  132.     
  133.     if (Scanning) {
  134.         NSetTrapAddress(savedTrapAddr, _UnmountVol, ToolTrap);
  135.     } else {
  136.         main_SetPort();
  137.         vol_Verify();
  138.         misc_HiliteScan();
  139.     };
  140.     
  141.     return true;
  142. }
  143.     
  144. /*______________________________________________________________________
  145.  
  146.     PageSetup - Process Page Setup Command.
  147. _____________________________________________________________________*/
  148.  
  149. static void PageSetup (void)
  150.  
  151. {
  152.     misc_ValPrint(&Prefs.helpPrint, false);
  153.     Prefs.helpPrint.menuPick = MenuPick;
  154.     rpp_StlDlog(&Prefs.helpPrint);
  155. }
  156.     
  157. /*______________________________________________________________________
  158.  
  159.     Print - Process Print Command.
  160.     
  161.     Entry:    printOne = true if Print One command.
  162.     
  163.     Exit:        function result = error code.
  164. _____________________________________________________________________*/
  165.  
  166.  
  167. static OSErr Print (Boolean printOne)
  168.  
  169. {
  170.     Str255            titleTmpl;    /* template for date, time, pnum in headers */
  171.     Str255            docName;        /* document name */
  172.     
  173.     GetIndString(titleTmpl, strListID, hlpDocTmpl);
  174.     GetIndString(docName, strListID, hlpDocTitle);
  175.     Prefs.helpPrint.title = docName;
  176.     Prefs.helpPrint.titleTmpl = titleTmpl;
  177.     Prefs.helpPrint.docName = docName;
  178.     misc_ValPrint(&Prefs.helpPrint, false);
  179.     Prefs.helpPrint.menuPick = MenuPick;
  180.     return hlp_Print(&Prefs.helpPrint, printOne);
  181. }
  182.     
  183. /*______________________________________________________________________
  184.  
  185.     Adjust - Adjust Menus.
  186. _____________________________________________________________________*/
  187.  
  188.  
  189. static void Adjust (void)
  190.  
  191. {
  192.     MenuHandle                fileM;                    /* handle to file menu */
  193.     MenuHandle                editM;                    /* handle to edit menu */
  194.     MenuHandle                scanM;                    /* handle to scan menu */
  195.     MenuHandle                disinfectM;                /* handle to disinfect menu */
  196.     
  197.     fileM = GetMHandle(fileMID);
  198.     editM = GetMHandle(editMID);
  199.     scanM = GetMHandle(scanMID);
  200.     disinfectM = GetMHandle(disinfectMID);
  201.     EnableItem(fileM, closeCommand);
  202.     EnableItem(fileM, saveAsCommand);
  203.     EnableItem(fileM, pageSetupCommand);
  204.     EnableItem(fileM, printCommand);
  205.     EnableItem(fileM, printOneCommand);
  206.     if (Scanning) {
  207.         DisableItem(fileM, quitCommand);
  208.         DisableItem(scanM, 0);
  209.         DisableItem(disinfectM, 0);
  210.     } else {
  211.         EnableItem(fileM, quitCommand);
  212.         EnableItem(scanM, 0);
  213.         EnableItem(disinfectM, 0);
  214.     };
  215.     DisableItem(editM, 0);
  216. }
  217.  
  218. /*______________________________________________________________________
  219.  
  220.     DrawTconTitle - Draw Table of Contents Title.
  221. _____________________________________________________________________*/
  222.  
  223.  
  224. static void DrawTconTitle ()
  225.  
  226. {
  227.     Str255            str;                /* title */
  228.     short                h;                    /* horizontal coord of title */
  229.  
  230.     GetIndString(str, strListID, tconTitle);
  231.     TextFace(bold);
  232.     h = (RectList[tconRect].left + RectList[tconRect].right
  233.         - StringWidth(str)) >> 1;
  234.     MoveTo(h, RectList[tconRect].top-5);
  235.     DrawString(str);
  236.     TextFace(normal);
  237. }
  238.     
  239. /*______________________________________________________________________
  240.  
  241.     help_Open - Open Help Window.
  242.     
  243.     Entry:        tag = Tagged line to jump to, or 0 if none.
  244. _____________________________________________________________________*/
  245.  
  246.  
  247. void help_Open (short tag)
  248.  
  249. {
  250.     hlp_PBlock        p;                /* hlp_Open param block. */
  251.     short                fNum;            /* font number */
  252.     
  253.     HelpMode = false;
  254.     misc_SetCursor();
  255.     
  256.     /* If the window is already open, activate it and jump to the tag. */
  257.     
  258.     if (HelpWindow) {
  259.         SelectWindow(HelpWindow);
  260.         if (tag) hlp_Jump(tag);
  261.         return;
  262.     };
  263.     
  264.     /* Get the help window and restore its state. */
  265.     
  266.     HelpWindow = wstm_Restore(false, helpWindID, (Ptr)&HelpWRecord,
  267.         &Prefs.helpState);
  268.     SetPort(HelpWindow);
  269.     
  270.     /* Initialize the window object. */
  271.     
  272.     ((WindowPeek)HelpWindow)->refCon = (long)&HelpWindObject;
  273.     HelpWindObject.windKind = helpWind;
  274.     HelpWindObject.moved = Prefs.helpState.moved;
  275.     SetRect(&HelpWindObject.sizeRect, HelpWindow->portRect.right, 
  276.         minHelpSize, HelpWindow->portRect.right, 0x7fff);
  277.     HelpWindObject.update = hlp_Update;
  278.     HelpWindObject.activate = hlp_Activate;
  279.     HelpWindObject.deactivate = hlp_Deactivate;
  280.     HelpWindObject.click = Click;
  281.     HelpWindObject.help = Help;
  282.     HelpWindObject.grow = hlp_Grow;
  283.     HelpWindObject.zoom = hlp_Zoom;
  284.     HelpWindObject.key = hlp_Key;
  285.     HelpWindObject.close = Close;
  286.     HelpWindObject.disk = nil;
  287.     HelpWindObject.save = Save;
  288.     HelpWindObject.pageSetup = PageSetup;
  289.     HelpWindObject.print = Print;
  290.     HelpWindObject.edit = nil;
  291.     HelpWindObject.adjust = Adjust;
  292.     HelpWindObject.periodic = nil;
  293.     HelpWindObject.dialogPre = nil;
  294.     HelpWindObject.dialogPost = nil;
  295.     
  296.     /* Initialize hlp_Open param block. */
  297.     
  298.     if (!utl_GetFontNumber("\pGeneva", &fNum)) fNum = applFont;
  299.     p.scrollLine = Prefs.helpScrollPos;
  300.     p.firstStrID = firstDocID;
  301.     p.listDefID = lDefID;
  302.     p.textRect = RectList[docRect];
  303.     p.fontNum = fNum;
  304.     p.fontSize = 9;
  305.     p.tabConID = tconID;
  306.     p.tabConRect = RectList[tconRect];
  307.     p.tabConFNum = fNum;
  308.     p.tabConFSize = 9;
  309.     p.tabConLSep = 12;
  310.     p.tag = tag;
  311.     p.tagRezID = tagID;
  312.     p.cellRezID = cellID;
  313.     p.cellOption = 2;
  314.     p.extraUpdate = DrawTconTitle;
  315.     
  316.     /* Call hlp_Open to complete the open. */
  317.     
  318.     hlp_Open(HelpWindow, &p);
  319. }
  320.